#coding: utf-8
class CulturalHeritage::CareAndMaintenancesController < ApplicationController

  @@institucion_cultural_roles = [1]
  def self.institucion_cultural_roles
    @@institucion_cultural_roles
  end
  @@sup_inc_roles = [8]
  def self.sup_inc_roles
    @@sup_inc_roles
  end
  @@exp_inc_roles = [9]
  def self.exp_inc_roles
    @@exp_inc_roles
  end

  before_filter :authenticate_user!
  # GET /cultural_heritage/care_and_maintenances
  # GET /cultural_heritage/care_and_maintenances.xml
  def index
    if (!user_signed_in? or !(@@institucion_cultural_roles.concat(@@sup_inc_roles).concat(@@exp_inc_roles).include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end
    
    @title_view = 'Cuidado y Mantenimiento'

    if (user_signed_in? && (@@sup_inc_roles.include?(current_user.role)))#INC Sup
      @cultural_heritage_care_and_maintenances =
        CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.status = ?",0])
    elsif (user_signed_in? && (@@exp_inc_roles.include?(current_user.role)))#INC Exp
      @cultural_heritage_care_and_maintenances =
        CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.expert = ? AND cultural_heritage_care_and_maintenances.status = ?",current_user.id,0])
    elsif (user_signed_in? && (@@institucion_cultural_roles.include?(current_user.role)))#MIC
      @cultural_heritage_care_and_maintenances =
        CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.responsible = ?",current_user.id])
    else
      @cultural_heritage_care_and_maintenances = []
    end
#    @cultural_heritage_care_and_maintenances = CulturalHeritage::CareAndMaintenance.all

    @lista_heritage_select=CulturalHeritage::CulturalHeritage.all
    @lista_institution_select=CulturalHeritage::CulturalInstitution.all
    @security_person=Security::Person.all
    @care_and_maintenance_experts=User.find(:all,:conditions=>["role = ?",@@exp_inc_roles])

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cultural_heritage_care_and_maintenances }
    end
  end

  # GET /cultural_heritage/care_and_maintenances/1
  # GET /cultural_heritage/care_and_maintenances/1.xml
  def show
    if (!user_signed_in? or !(@@institucion_cultural_roles.concat(@@sup_inc_roles).concat(@@exp_inc_roles).include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end
    
    @title_view = 'Cuidado y Mantenimiento'
    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.find(params[:id])

#    @cultural_heritage_care_and_maintenance.start_date = DateTime.strptime(@cultural_heritage_care_and_maintenance.start_date, "%d/%m/%Y")
#    @cultural_heritage_care_and_maintenance.end_date = DateTime.strptime(@cultural_heritage_care_and_maintenance.end_date, "%d/%m/%Y")

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @cultural_heritage_care_and_maintenance }
    end
  end

  # GET /cultural_heritage/care_and_maintenances/new
  # GET /cultural_heritage/care_and_maintenances/new.xml
  def new
    if (!user_signed_in? or !(@@institucion_cultural_roles.concat(@@sup_inc_roles).include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end
    
    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.new
    @title_view = 'Cuidado y Mantenimiento'
    @cultural_heritages = CulturalHeritage::CulturalHeritage.all
    @cultural_institutions = CulturalHeritage::CulturalInstitution.all
    @cultural_care_and_maintenance_types = CulturalHeritage::CareAndMaintenanceType.all
    @security_people = Security::Person.all
    @care_and_maintenance_experts=User.find(:all,:conditions=>["role = ?",@@exp_inc_roles])

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @cultural_heritage_care_and_maintenance }
    end
  end

  # GET /cultural_heritage/care_and_maintenances/1/edit
  def edit
    if (!user_signed_in? or !(@@institucion_cultural_roles.concat(@@sup_inc_roles).include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end
    
    @title_view = 'Cuidado y Mantenimiento'
    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.find(params[:id])

#    @cultural_heritage_care_and_maintenance.start_date = DateTime.strptime(@cultural_heritage_care_and_maintenance.start_date, "%d/%m/%Y")
#    @cultural_heritage_care_and_maintenance.end_date = DateTime.strptime(@cultural_heritage_care_and_maintenance.end_date, "%d/%m/%Y")

    @cultural_heritages = CulturalHeritage::CulturalHeritage.all
    @cultural_institutions = CulturalHeritage::CulturalInstitution.all
    @cultural_care_and_maintenance_types = CulturalHeritage::CareAndMaintenanceType.all
    @security_people=Security::Person.all
    @care_and_maintenance_experts=User.find(:all,:conditions=>["role = ?",@@exp_inc_roles])

    @lalala=@cultural_heritage_care_and_maintenance.mayor

#    @care_and_maintenance_mayors = Array.new() { [{id=>0, name=>"Menor"},{id=>1, name=>"Mayor"}]  }
  end

  # POST /cultural_heritage/care_and_maintenances
  # POST /cultural_heritage/care_and_maintenances.xml
  def create
    if (!user_signed_in? or !(@@institucion_cultural_roles.concat(@@sup_inc_roles).include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end
    
    @title_view = 'Cuidado y Mantenimiento'

    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.new(params[:cultural_heritage_care_and_maintenance])

    @cultural_heritages = CulturalHeritage::CulturalHeritage.all
    @cultural_institutions = CulturalHeritage::CulturalInstitution.all
    @cultural_care_and_maintenance_types = CulturalHeritage::CareAndMaintenanceType.all
    @security_people=Security::Person.all
    @care_and_maintenance_experts = User.find(:all,:conditions=>["role = ?",@@exp_inc_roles])
    
    respond_to do |format|
      if @cultural_heritage_care_and_maintenance.save
        format.html { redirect_to(@cultural_heritage_care_and_maintenance, :notice => 'El Cuidado y Mantenimiento fue registrado satisfactoriamente.') }
        format.xml  { render :xml => @cultural_heritage_care_and_maintenance, :status => :created, :location => @cultural_heritage_care_and_maintenance }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @cultural_heritage_care_and_maintenance.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /cultural_heritage/care_and_maintenances/1
  # PUT /cultural_heritage/care_and_maintenances/1.xml
  def update
    if (!user_signed_in? or !(@@institucion_cultural_roles.concat(@@sup_inc_roles).include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end
    
    @title_view = 'Cuidado y Mantenimiento'
    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.find(params[:id])

#    @cultural_heritage_care_and_maintenance.start_date = DateTime.strptime(@cultural_heritage_care_and_maintenance.start_date, "%d/%m/%Y")
#    @cultural_heritage_care_and_maintenance.end_date = DateTime.strptime(@cultural_heritage_care_and_maintenance.end_date, "%d/%m/%Y")
    
    @cultural_heritages = CulturalHeritage::CulturalHeritage.all
    @cultural_institutions = CulturalHeritage::CulturalInstitution.all
    @cultural_care_and_maintenance_types = CulturalHeritage::CareAndMaintenanceType.all
    @security_person=Security::Person.all
    @care_and_maintenance_experts =  User.find(:all,:conditions=>["role = ?",@@exp_inc_roles])
    respond_to do |format|
      if @cultural_heritage_care_and_maintenance.update_attributes(params[:cultural_heritage_care_and_maintenance])
        format.html { redirect_to(@cultural_heritage_care_and_maintenance, :notice => 'El Cuidado y Mantenimiento fue actualizado satisfactoriamente.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @cultural_heritage_care_and_maintenance.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /cultural_heritage/care_and_maintenances/1
  # DELETE /cultural_heritage/care_and_maintenances/1.xml
  def destroy
    if (!user_signed_in? or !(@@institucion_cultural_roles.concat(@@sup_inc_roles).include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end

    @cultural_heritage_care_and_maintenance = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @cultural_heritage_care_and_maintenance.destroy

    respond_to do |format|
      format.html { redirect_to(cultural_heritage_care_and_maintenances_url) }
      format.xml  { head :ok }
    end
  end



  def update_table
    @search_text = params[:search]
#    @search_combo = params[:search_complaint_type]

       #Instituciones culturales :: OJO, eliminar cultural description cuando la carga sea muy alta

    @care_and_maintenance_types =
        CulturalHeritage::CareAndMaintenanceType.find(
          :all,
          :conditions=>["cultural_heritage_care_and_maintenance_types.name LIKE ? ", "%"+@search_text.to_s+"%"]
        );

    @cultural_institutions =
        CulturalHeritage::CulturalInstitution.find(
          :all,
          :conditions=>["cultural_heritage_cultural_institutions.name LIKE ? or cultural_heritage_cultural_institutions.description LIKE ?", "%"+@search_text.to_s+"%", "%"+@search_text.to_s+"%"]
        );

    @care_and_maintenance_cultural_heritages =
        CulturalHeritage::CulturalHeritage.find(
          :all,
          :conditions=>["cultural_heritage_cultural_heritages.nationalRegistrationCode LIKE ? OR cultural_heritage_cultural_heritages.name LIKE ? OR cultural_heritage_cultural_heritages.description LIKE ?", "%"+@search_text.to_s+"%", "%"+@search_text.to_s+"%", "%"+@search_text.to_s+"%"]
        );

    #Ahora busco dicho texto en los campos ed tipo string de Collections
    if ! @search_text.blank?
#      if ! @search_combo.blank?
#        @cultural_heritage_collections = CulturalHeritage::Collection.find(:all,:conditions=>["cultural_heritage_collections.id = ? and cultural_heritage_collections.complaint_type = ? ",@search_text,@search_combo])
#      else
      if (user_signed_in? && (@@sup_inc_roles.include?(current_user.role)))#INC Sup
        @cultural_heritage_care_and_maintenances =
          CulturalHeritage::CareAndMaintenance.find(
            :all,
            :conditions=>
                [ "cultural_heritage_care_and_maintenances.status = ? AND " +
                  "cultural_heritage_care_and_maintenances.id = ? OR " +
                  "cultural_heritage_care_and_maintenances.description LIKE ? OR " +
                  "cultural_heritage_care_and_maintenances.cultural_institution in (?) OR " +
                  "cultural_heritage_care_and_maintenances.care_and_maintenance_type in (?) OR " +
                  "cultural_heritage_care_and_maintenances.cultural_heritage in (?)",
                  0,
                  @search_text,
                  "%"+@search_text.to_s+"%",
                  (@cultural_institutions.empty? ? '' : @cultural_institutions.map(&:id)),
                  (@care_and_maintenance_types.empty? ? '' : @care_and_maintenance_types.map(&:id)),
                  (@care_and_maintenance_cultural_heritages.empty? ? '' : @care_and_maintenance_cultural_heritages.map(&:id))
                ]
          )
      elsif (user_signed_in? && (@@exp_inc_roles.include?(current_user.role)))#INC Exp
        @cultural_heritage_care_and_maintenances =
          CulturalHeritage::CareAndMaintenance.find(
            :all,
            :conditions=>
              [ "cultural_heritage_care_and_maintenances.expert = ? AND cultural_heritage_care_and_maintenances.status = ? AND " +
                "cultural_heritage_care_and_maintenances.id = ? OR " +
                "cultural_heritage_care_and_maintenances.description LIKE ? OR " +
                "cultural_heritage_care_and_maintenances.cultural_institution in (?) OR " +
                "cultural_heritage_care_and_maintenances.care_and_maintenance_type in (?) OR " +
                "cultural_heritage_care_and_maintenances.cultural_heritage in (?)",
                current_user.id,0,
                @search_text,
                "%"+@search_text.to_s+"%",
                (@cultural_institutions.empty? ? '' : @cultural_institutions.map(&:id)),
                (@care_and_maintenance_types.empty? ? '' : @care_and_maintenance_types.map(&:id)),
                (@care_and_maintenance_cultural_heritages.empty? ? '' : @care_and_maintenance_cultural_heritages.map(&:id))
              ]
          )
      elsif (user_signed_in? && (@@institucion_cultural_roles.include?(current_user.role)))#MIC
        @cultural_heritage_care_and_maintenances =
          CulturalHeritage::CareAndMaintenance.find(
            :all,
            :conditions=>
              [ "cultural_heritage_care_and_maintenances.responsible = ? AND " +
                "cultural_heritage_care_and_maintenances.id = ? OR " +
                "cultural_heritage_care_and_maintenances.description LIKE ? OR " +
                "cultural_heritage_care_and_maintenances.cultural_institution in (?) OR " +
                "cultural_heritage_care_and_maintenances.care_and_maintenance_type in (?) OR " +
                "cultural_heritage_care_and_maintenances.cultural_heritage in (?)",
                current_user.id,
                @search_text,
                "%"+@search_text.to_s+"%",
                (@cultural_institutions.empty? ? '' : @cultural_institutions.map(&:id)),
                (@care_and_maintenance_types.empty? ? '' : @care_and_maintenance_types.map(&:id)),
                (@care_and_maintenance_cultural_heritages.empty? ? '' : @care_and_maintenance_cultural_heritages.map(&:id))
              ]
          )
      end
#          @cultural_heritage_care_and_maintenances =
#            CulturalHeritage::CareAndMaintenance.find(
#              :all,
#              :conditions=>
#                [ "cultural_heritage_care_and_maintenances.id = ? OR " +
#                  "cultural_heritage_care_and_maintenances.description LIKE ? OR " +
#                  "cultural_heritage_care_and_maintenances.cultural_institution in (?) OR " +
#                  "cultural_heritage_care_and_maintenances.care_and_maintenance_type in (?) OR " +
#                  "cultural_heritage_care_and_maintenances.cultural_heritage in (?)",
#                  @search_text,
#                  "%"+@search_text.to_s+"%",
#                  (@cultural_institutions.empty? ? '' : @cultural_institutions.map(&:id)),
#                  (@care_and_maintenance_types.empty? ? '' : @care_and_maintenance_types.map(&:id)),
#                  (@care_and_maintenance_cultural_heritages.empty? ? '' : @care_and_maintenance_cultural_heritages.map(&:id))
#                ]
#              )
#      end
    else
#      if !@search_combo.blank?
#        @cultural_heritage_collections = CulturalHeritage::Collection.find(:all,:conditions=>["cultural_heritage_collections.complaint_type = ? ",@search_combo])
#      else
        if (user_signed_in? && (@@sup_inc_roles.include?(current_user.role)))#INC Sup
          @cultural_heritage_care_and_maintenances =
            CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.status = ?",0])
        elsif (user_signed_in? && (@@exp_inc_roles.include?(current_user.role)))#INC Exp
          @cultural_heritage_care_and_maintenances =
            CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.expert = ? AND cultural_heritage_care_and_maintenances.status = ?",current_user.id,0])
        elsif (user_signed_in? && (@@institucion_cultural_roles.include?(current_user.role)))#MIC
          @cultural_heritage_care_and_maintenances =
            CulturalHeritage::CareAndMaintenance.find(:all,:conditions=>["cultural_heritage_care_and_maintenances.responsible = ?",current_user.id])
        else
          @cultural_heritage_care_and_maintenances = []
        end
#      end
    end
    render :partial => "table"
  end

  def is_mayor
    if (!user_signed_in? or !(@@sup_inc_roles.include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end
    
    @care = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @care.mayor=1
    @care.save
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_care_and_maintenances_url) }
      format.xml  { head :ok }
    end

#    IncidenceMailer.attend_email(@incidence).deliver
  end

  def proceed
    if (!user_signed_in? or !(@@exp_inc_roles.concat(@@sup_inc_roles).include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end
    
    @care = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @care.status=1

    if (@@exp_inc_roles.include?(current_user.role))
      @care.expert = current_user.id
    elsif (@@sup_inc_roles.include?(current_user.role))
      @care.supervisor = current_user.id
    end

    @care.save
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_care_and_maintenances_url) }
      format.xml  { head :ok }
    end

#    IncidenceMailer.attend_email(@incidence).deliver
  end

  def notapplicable
    if (!user_signed_in? or !(@@exp_inc_roles.concat(@@sup_inc_roles).include?(current_user.role)) )
      respond_to do |format|
        format.html { redirect_to(new_user_session_path) }
      end
      return
    end
    
    @care = CulturalHeritage::CareAndMaintenance.find(params[:id])
    @care.status=0

    if (@@exp_inc_roles.include?(current_user.role))
      @care.expert = current_user.id
    elsif (@@sup_inc_roles.include?(current_user.role))
      @care.supervisor = current_user.id
    end
    
    @care.save
    respond_to do |format|
      format.html { redirect_to(cultural_heritage_care_and_maintenances_url) }
      format.xml  { head :ok }
    end

  end
end
